package assist.datastore.dao;

import java.util.ArrayList;
import java.util.Collection;

import assist.domain.model.Shot;
import assist.domain.model.Shots;

/**
 * A Base class DAO that can be used to CRUD Shot objects.  Since we are using
 * concrete-table-inheritance, there is no Table in the datastore for Shot 
 * objects.  So this class must use the concrete DAO objects of all the 
 * underlying subclass types to actually do the CRUD operations.
 */
public class ShotDAO extends AbstractDAO<Shot> implements EventFinder<Shot>
{
    private ShotOnGoalDAO shotOnGoalDAO;
    private BlockedShotDAO blockedShotDAO;
    private MissedShotDAO missedShotDAO;
    private GoalDAO goalDAO;
  
    protected ShotDAO()
    {
        super(Shot.class);
        DAOFactory daoFactory = DAOFactory.getDAOFactory();
        this.shotOnGoalDAO = daoFactory.getShotOnGoalDAO();
        this.blockedShotDAO = daoFactory.getBlockedShotDAO();
        this.missedShotDAO = daoFactory.getMissedShotDAO();
        this.goalDAO = daoFactory.getGoalDAO();
    }

    @Override
    public Shot save(final Shot shot)
    {
        //Save using the appropriate DAO.        
        return null;
    }

    @Override
    public Collection<Shot> saveAll(Collection<Shot> objects)
    {
        // TODO: Iterate the collection, and save each instance individually.
        // Put in comment that it is preferable to use Shots instead.
        return null;
    }
    
    /**
     * Convienience method for saving as Shots.
     */   
    public Shots saveAll(final Shots shots)
    {
        this.blockedShotDAO.saveAll(shots.getBlocked());
        this.shotOnGoalDAO.saveAll(shots.getOnGoal());
        this.missedShotDAO.saveAll(shots.getMissed());
        this.goalDAO.saveAll(shots.getGoals());        
        return shots;
    }    

    @Override
    public Shot getById(final Object id)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public Shots findShotsByGameId(final String gameId)
    {
        Shots shots = new Shots();
        shots.addAll(this.findByGameId(gameId));
        return shots;
    } 
    
    @Override
    public Collection<Shot> findByGameId(String gameId)
    {
        Collection<Shot> shots = new ArrayList<Shot>();
        shots.addAll(this.blockedShotDAO.findByGameId(gameId));
        shots.addAll(this.shotOnGoalDAO.findByGameId(gameId));
        shots.addAll(this.missedShotDAO.findByGameId(gameId));
        shots.addAll(this.goalDAO.findByGameId(gameId)); 
        return shots;
    }

}
